<template>
  <div>
    <div class="provider-list">
      <div class="header">
        <div class="header-line1 flex-s">
          <el-button type="primary" @click="onOperation({}, 'add')"
            ><el-icon style="margin-right: 2px"><Plus /></el-icon
            >新建项目</el-button
          >
          <el-button
            ><el-icon style="margin-right: 2px"><Download /></el-icon
            >导入项目</el-button
          >
        </div>
        <div class="header-line2 flex-s">
          <el-input
            v-model="state.params.ziliaomingcheng"
            placeholder="请输入内容"
            style="width: 350px"
            size="large"
          >
            <template #append>
              <el-button
                @click="GetList"
                style="min-width: 50px"
                icon="Search"
              />
            </template>
          </el-input>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="state.params.leixing"
            size="large"
            placeholder="类型"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in state.option"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            >
              {{ item.dicLabel }}
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="content content-table">
        <el-table
          ref="multipleTableRef"
          border
          :data="state.list"
          style="width: calc(100% - 20px)"
        >
          <el-table-column
            property="id"
            label="序号"
            width="100"
            align="center"
          />
          <el-table-column property="ziliaomingcheng" label="资料名称" align="center" />
          <el-table-column
            property="leixing"
            label="类型"
            width="130"
            align="center"
          />
          <el-table-column
            property="createTime"
            label="创建时间"
            width="300"
            align="center"
          />
          <el-table-column
            property="yueduliang"
            label="阅读量"
            width="200"
            align="center"
          />
          <el-table-column
            property="xiazailiang"
            label="下载量"
            width="200"
            align="center"
          />
          <el-table-column
            fixed="right"
            label="操作"
            width="250"
            align="center"
          >
            <template #default="scope">
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'detail')"
                >查看</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <h-page
          v-if="state.total"
          v-model:curr-page="state.params.current"
          :size="state.params.size"
          :total="state.total"
          @change="GetList"
        />
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { GetDatabaseList } from "@/api/database";
import { GetDictionaryTree } from "@/api/system";
const route = useRoute();
const router = useRouter();
const state = reactive({
  params: {
    current: Number(route.query.page) || 1,
    size: Number(route.query.size) || 10,
    ziliaomingcheng: "",
    leixing: "",
  },
  list: [],
  option: [],
  total: 0,
});
// 资料库类型下拉查询
const GetType = async () => {
  const res = await GetDictionaryTree({ dicModel: "ziliaokuleixing" });
  if (res.code == 4201) {
    state.option = res.data;
  }
};
const GetList = async () => {
  const res = await GetDatabaseList(state.params);
  state.list = res.data.records;
  state.total = res.data.total;
};
// 操作
const onOperation = (params, operation) => {
  if (operation === "detail") {
    router.push({
      path: `/ziliaoku/detail`,
      query: {
        id: params.id,
      },
    });
  }
  if (operation === "add" || operation === "edit") {
    router.push({
      path: `/ziliaoku/info`,
      query: {
        id: params.id || null,
        type: operation,
      },
    });
  }
};
watch(
  () => route.query,
  () => {
    state.params.current = Number(route.query.page) || 1;
    GetList();
  },
  { deep: true }
);
GetList();
GetType();
</script>
<style lang="scss" scoped>
.header {
  border-bottom: 10px solid #f2f2f2;
  border-radius: 10px 10px 0 0;
}
.header-line1 {
  padding: 20px 10px;
  border-bottom: 1px solid #ccc;
  border-radius: 10px 10px 0 0;
}
.header-line2 {
  padding: 20px 10px;
}
.content {
  border: none !important;
  margin: 20px 10px;
  background-color: transparent !important;
  padding-bottom: 30px;
}
</style>
<style lang="scss">
.content-table .el-table__header th {
  background-color: #f2f9fc !important;
}
</style>
